import { Component, OnInit ,Input,Output,Host,Inject,forwardRef,EventEmitter } from '@angular/core';
import {TranslateService} from '@ngx-translate/core';
import {DragdropService} from '../../services/dragdrop.service'

@Component({
  selector: 'app-group',
  templateUrl: './group.component.html',
  styleUrls: ['./group.component.css']
})
export class GroupComponent implements OnInit {

  helpMessage={}
  curWidget:any;
  dimensionGroup=[]
  availables=[]
  draggedEl=false;
  draggedArray:any;      //保存拖过来的数据，传给表单

  constructor(
      public translate:TranslateService,
      private dragdropservice:DragdropService
    ) { 
    translate.addLangs(['zh','en']);                          //添加语言支持
    translate.setDefaultLang('en');                           //设置默认语言
    let broswerLang = translate.getBrowserLang();              //获取浏览器支持的语言
    console.log(broswerLang)                                   //zh
    translate.use(broswerLang.match(/zh|en/) ? broswerLang : 'en');

  }


  
  @Output() group=new EventEmitter<any>();
  ngOnInit() {

  }

  findIndex(el:any) {
    let index = -1;
    for(let i = 0; i < this.dimensionGroup.length; i++) {
        if(el.vin === this.dimensionGroup[i].vin) {
            index = i;
            break;
        }
    }
    return index;
  }
  droped(el){
      let t=this.dragdropservice.getdrag()
      console.log(t)
      let tcol = this.dragdropservice.getdragcol();
      console.log(tcol)

      console.log(t.b._events[0])        //car_type 拖动的名字
  
      if(t.a._events[0]&&t.b._events[0]=='level'){
        t.a._events[0]['other']='a';
        console.log(t.a._events[0])
        this.dimensionGroup = [...this.dimensionGroup,t.a._events[0]];
        console.log(this.dimensionGroup)
        let draggedCarIndex = this.findIndex(t.a._events[0]);
        t.c._events[0] = t.c._events[0].filter((val,i) => i!=draggedCarIndex);
        t.a._events[0]= null;
      }
      
     
      // let draggedCarIndex = this.findIndex(t.a._events[0]);
      // t.a._events[0] = t.a._events[0].filter((val,i) => i!=draggedCarIndex);
     

      if(tcol.a._events[0]&&tcol.b._events[0]=='column'){
        tcol.a._events[0]['other']='b';
        console.log(tcol.a._events[0])
        this.dimensionGroup = [...this.dimensionGroup,tcol.a._events[0]]
        console.log(this.dimensionGroup)
        let draggedCarIndex = this.findIndex(tcol.a._events[0]);
        tcol.c._events[0] = tcol.c._events[0].filter((val,i) => i!=draggedCarIndex);
        tcol.a._events[0]= null;
      }

      console.log(this.dimensionGroup)
      this.group.emit(this.dimensionGroup)
      for(let i=0;i<this.dimensionGroup.length;i++){
        if(this.dimensionGroup[i]['column']==this.dimensionGroup[i+1]['column']){
            alert('不能重复拖动同一数据集')
        }
      }

      
      
  }
  emitColumn(el){
      console.log(el)
      console.log(this.dimensionGroup)
      //点击删除时调用backColumn
        this.dragdropservice.dropEl.emit(el)
  }

  console(){
    console.log(this.dimensionGroup)
  }

 

}
